/**   
* @Title: PaiDanSheZhiServiceImpl.java 
* @Package com.zjdiepu.www.biz.zpp 
* 
* @author zhangpp
* @date 2017年6月1日 下午4:26:23 
* @version V1.0   
*/
package com.zjdiepu.www.biz.zpp;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.fk.FkAuditListMapper;
import com.zjdiepu.www.dao.zpp.YwShenQingZxDao;
import com.zjdiepu.www.ibiz.zpp.PaiDanSheZhiService;
import com.zjdiepu.www.model.fk.dto.FkAuditList;
import com.zjdiepu.www.model.fk.ext.FkAuditListExt;
import com.zjdiepu.www.model.zpp.fksh.vo.ParamsVo;
import com.zjdiepu.www.model.zpp.pub.ext.ParamsExt;

/**      
 * 项目名称：diepu-parent-biz   
 * 类名称：PaiDanSheZhiServiceImpl   
 * 类描述：   
 * 创建人：zhangpp
 * 创建时间：2017年6月1日 下午4:26:23   
 * 修改人：zhangpp
 * 修改时间：2017年6月1日 下午4:26:23   
 * 修改备注：   
 * @version V1.0     
 */
@Service
public class PaiDanSheZhiServiceImpl implements PaiDanSheZhiService{

	@Autowired
	private FkAuditListMapper fkAuditListMapper;
	
	@Autowired
	private YwShenQingZxDao YwShenQingZxDao;
	
	
	/* 
	* @author zhangpp
	* <p>Title: paiDanSheZhiDianShenYiShenPaiDanSave</p> 
	* <p>Description: </p> 
	* @param paramsExt
	* @param currentUser 
	* @see com.zjdiepu.www.ibiz.zpp.PaiDanSheZhiService#paiDanSheZhiDianShenYiShenPaiDanSave(com.zjdiepu.www.model.zpp.pub.ext.ParamsExt, com.zjdiepu.www.base.core.AbstractSysUser) 
	* @version V1.0
	*/
	@Override
	public void diepuPaiDanSheZhiDianShenYiShenPaiDanSave(ParamsExt paramsExt,
			AbstractSysUser currentUser) {
		
		
		String id = currentUser.getId();
		String name = currentUser.getName();
		
		
		if(null == paramsExt)
		{
			throw new BaseException(ResultFlagType.PARAM_EMPTY.getCode().toString(),ResultFlagType.PARAM_EMPTY.getValue());
		}
		
		//指定分配
		if(paramsExt.getOpCode().equals("2"))
		{
			//修改 fk_audit_list 状态   以及插入数据
			String[] temp = paramsExt.getStr().split(",");
			if(0 == temp.length)
			{
				throw new BaseException(ResultFlagType.PARAM_EMPTY.getCode().toString(),ResultFlagType.PARAM_EMPTY.getValue());
			}
			
			for(int i=0;i<temp.length;i++)
			{
				FkAuditList fkAuditList = new FkAuditList();
				fkAuditList.initUpdateData(id, name);
				fkAuditList.setId(temp[i]);
				fkAuditList.setOperatorId(paramsExt.getOperator_id());
				fkAuditList.setOperatorName(paramsExt.getOperator_name());
				fkAuditList.setAuditListStatus(1);
				int updateCount = fkAuditListMapper.updateByPrimaryKeySelective(fkAuditList);
				if(updateCount == 0)
				{
					throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
				} 
				else if(updateCount > 1)
				{
					throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
				}
			}
			return;
		}
		
		//随机分配
		if(paramsExt.getOpCode().equals("1"))
		{
			//查询业务员数据信息
			List<ParamsVo> paramsVo = YwShenQingZxDao.getOpList(null);
			
			
			//查询需要分配的数据
			FkAuditListExt fkAuditListExt = new FkAuditListExt();
			fkAuditListExt.setModeId("M_dsys");
			fkAuditListExt.setAuditListStatus(5);
			List<FkAuditList> list = fkAuditListMapper.selectByExt(fkAuditListExt);
			
			int j = 0;
			
			if(list.size() >= paramsExt.getAllocationNum())
			{
				for(int i=0;i<paramsExt.getAllocationNum();i++)
				{
					FkAuditList fkAuditList = new FkAuditList();
					fkAuditList.initUpdateData(id, name);
					fkAuditList.setId(list.get(i).getId());
					
					if(j >= paramsVo.size())
					{
						j = 0;
					}
					
					fkAuditList.setOperatorId(paramsVo.get(j).getReplaceId());
					fkAuditList.setOperatorName(paramsVo.get(j).getUname());
					fkAuditList.setAuditListStatus(1);
					
					int updateCount = fkAuditListMapper.updateByPrimaryKeySelective(fkAuditList);
					if(updateCount == 0)
					{
						throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
					} 
					else if(updateCount > 1)
					{
						throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
					}
				}
			}
			return;
		}
		throw new BaseException(ResultFlagType.PARAM_EMPTY.getCode().toString(),ResultFlagType.PARAM_EMPTY.getValue());
	}


	/* 
	* @author zhangpp
	* <p>Title: paiDanSheZhiDianShenErShenPaiDanSave</p> 
	* <p>Description: </p> 
	* @param paramsExt
	* @param currentUser 
	* @see com.zjdiepu.www.ibiz.zpp.PaiDanSheZhiService#paiDanSheZhiDianShenErShenPaiDanSave(com.zjdiepu.www.model.zpp.pub.ext.ParamsExt, com.zjdiepu.www.base.core.AbstractSysUser) 
	* @version V1.0
	*/
	@Override
	public void diepuPaiDanSheZhiDianShenErShenPaiDanSave(ParamsExt paramsExt,
			AbstractSysUser currentUser) {
		
		String id = currentUser.getId();
		String name = currentUser.getName();
		
		
		if(null == paramsExt)
		{
			throw new BaseException(ResultFlagType.PARAM_EMPTY.getCode().toString(),ResultFlagType.PARAM_EMPTY.getValue());
		}
		
		//指定分配
		if(paramsExt.getOpCode().equals("2"))
		{
			//修改 fk_audit_list 状态   以及插入数据
			String[] temp = paramsExt.getStr().split(",");
			if(0 == temp.length)
			{
				throw new BaseException(ResultFlagType.PARAM_EMPTY.getCode().toString(),ResultFlagType.PARAM_EMPTY.getValue());
			}
			
			for(int i=0;i<temp.length;i++)
			{
				FkAuditList fkAuditList = new FkAuditList();
				fkAuditList.initUpdateData(id, name);
				fkAuditList.setId(temp[i]);
				fkAuditList.setOperatorId(paramsExt.getOperator_id());
				fkAuditList.setOperatorName(paramsExt.getOperator_name());
				fkAuditList.setAuditListStatus(1);
				int updateCount = fkAuditListMapper.updateByPrimaryKeySelective(fkAuditList);
				if(updateCount == 0)
				{
					throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
				} 
				else if(updateCount > 1)
				{
					throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
				}
			}
			return;
		}
		
		//随机分配
		if(paramsExt.getOpCode().equals("1"))
		{
			//查询业务员数据信息
			List<ParamsVo> paramsVo = YwShenQingZxDao.getOpList(null);
			
			
			//查询需要分配的数据
			FkAuditListExt fkAuditListExt = new FkAuditListExt();
			fkAuditListExt.setModeId("M_dses");
			fkAuditListExt.setAuditListStatus(5);
			List<FkAuditList> list = fkAuditListMapper.selectByExt(fkAuditListExt);
			
			int j = 0;
			
			if(list.size() >= paramsExt.getAllocationNum())
			{
				for(int i=0;i<paramsExt.getAllocationNum();i++)
				{
					FkAuditList fkAuditList = new FkAuditList();
					fkAuditList.initUpdateData(id, name);
					fkAuditList.setId(list.get(i).getId());
					
					if(j >= paramsVo.size())
					{
						j = 0;
					}
					
					fkAuditList.setOperatorId(paramsVo.get(j).getReplaceId());
					fkAuditList.setOperatorName(paramsVo.get(j).getUname());
					fkAuditList.setAuditListStatus(1);
					
					int updateCount = fkAuditListMapper.updateByPrimaryKeySelective(fkAuditList);
					if(updateCount == 0)
					{
						throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
					} 
					else if(updateCount > 1)
					{
						throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
					}
				}
			}
			return;
		}
		throw new BaseException(ResultFlagType.PARAM_EMPTY.getCode().toString(),ResultFlagType.PARAM_EMPTY.getValue());
	}
}
